﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using OverStore.Runtime.ObjectRepository.ErrorTransformer;
using OverStore.Configuration.ObjectRepository.ErrorTransformer;

namespace OverStore.Configuration.Builder.Fluent 
{
    /// <summary>
    /// Contains set of nested classes helps to configure error transformer.
    /// </summary>
    /// <typeparam name="TInstance">The type of the instance.</typeparam>
    /// <typeparam name="TNextStep">The type of the next step.</typeparam>
    public static class ErrorTransformerBuilder<TInstance, TNextStep>
        where TInstance : class
    {
        /// <summary>
        /// Builds <see cref="MatchErrorTransformerConfiguration{TInstance}"/>
        /// </summary>
        public class MatchErrorTransformerStep
        {
            private readonly MatchErrorTransformerConfiguration<TInstance> _Param;
            private readonly Func<MatchErrorTransformerConfiguration<TInstance>, TNextStep> _OnCompleted;

            /// <summary>
            /// Initializes a new instance of the <see cref="ErrorTransformerBuilder&lt;TInstance, TNextStep&gt;.MatchErrorTransformerStep"/> class.
            /// </summary>
            /// <param name="config">The config.</param>
            /// <param name="onCompleted">The on completed.</param>
            public MatchErrorTransformerStep(
                MatchErrorTransformerConfiguration<TInstance> config,
                Func<MatchErrorTransformerConfiguration<TInstance>, TNextStep> onCompleted)
            {
                if (Object.ReferenceEquals(config, null))
                    throw new ArgumentNullException("config");
                if (Object.ReferenceEquals(onCompleted, null))
                    throw new ArgumentNullException("onCompleted");
                _Param = config;
                _OnCompleted = onCompleted;
            }

            /// <summary>
            /// Transforms the exception of specified type which message matches specified pattern to
            /// PersistingOperationFailedException with resulting message.
            /// </summary>
            /// <typeparam name="TException">The type of the exception.</typeparam>
            /// <param name="pattern">The pattern.</param>
            /// <param name="matchType">Type of the match.</param>
            /// <param name="resultingMessage">The resulting message.</param>
            /// <returns></returns>
            public MatchErrorTransformerStep
                Transform<TException>(string pattern, MessageMatchType matchType, string resultingMessage)
                where TException : Exception
            {
                _Param.MatchCases.Add(new MatchCase(typeof(TException), pattern, matchType, resultingMessage));
                return this;
            }

            /// <summary>
            /// Ends configuration of the error transformer.
            /// </summary>
            public TNextStep EndErrorTransform()
            {
                return _OnCompleted(_Param);
            }
        }
    }
}
